package com.ruoyi.system.interAttr.attr;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MAIGEHAOAttr {

    public List<GamesRes> getGames(SalePartner salePartner) {
        List<GamesRes> resultList = new ArrayList<>();
        JSONObject jsonObject = new JSONObject();
        String gameServer = "http://account-api.maigehao.com/api/open/goods/gameList";
        JSONObject params = new JSONObject();
        jsonObject.put("appid", salePartner.getOther());
        String sign = DigestUtils.md5Hex(jsonObject+salePartner.getSaleKey());
        params.put("sign",sign);
        params.put("data",jsonObject);
        String response = HttpUtils.doPostForm(gameServer, params,null);
        JSONObject responseJson = JSONObject.parseObject(response);
        String success = responseJson.getString("message");
        if (success.equals("成功")) {
            List<JSONObject> data = JSONObject.parseArray(responseJson.getString("data"), JSONObject.class);
            if (CollectionUtils.isNotEmpty(data)) {
                for (JSONObject datum : data) {
                    GamesRes res = new GamesRes();
                    res.setId(datum.getString("id"));
                    res.setGameName(datum.getString("name"));
                    resultList.add(res);
                }
            }
        } else {
            throw new GlobalException("买个号游戏列表获取失败");
        }
        return resultList;
    }

    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        JSONObject jsonObject = new JSONObject();
        String gameServer = "http://account-api.maigehao.com/api/open/goods/gameServer";
        JSONObject params = new JSONObject();
        jsonObject.put("appid",salePartner.getOther());
        jsonObject.put("game_id",gameId);

        String sign = DigestUtils.md5Hex(jsonObject+salePartner.getSaleKey());
        params.put("sign",sign);
        params.put("data",jsonObject);
        System.out.println(params);
        String response = HttpUtils.doPostForm(gameServer, params,null);
        System.out.println("cooperateGameGoods : " + response);
        JSONObject responseJson = JSONObject.parseObject(response);
        String success = responseJson.getString("message");
        List<InterServerRes> resultList = new ArrayList<>();
        if (success.equals("成功")) {
            List<JSONObject> data = JSONObject.parseArray(responseJson.getString("data"), JSONObject.class);
            if (CollectionUtils.isNotEmpty(data)) {
                for (JSONObject datum : data) {
                    resultList.add(builderInterServerResChild(datum));
                }
            }
        } else {
            throw new GlobalException("买个号区服列表获取失败");
        }
        return resultList;
    }

    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        JSONObject jsonObject = new JSONObject();
        String gameProperty = "http://account-api.maigehao.com/api/open/goods/gameProperty";
        JSONObject params = new JSONObject();
        jsonObject.put("appid",salePartner.getOther());
        jsonObject.put("game_id",gameId);
        String sign = DigestUtils.md5Hex(jsonObject+salePartner.getSaleKey());
        params.put("sign",sign);
        params.put("data",jsonObject);
        String response = HttpUtils.doPostForm(gameProperty, params,null);
        JSONObject responseJson = JSONObject.parseObject(response);
        String success = responseJson.getString("message");
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        if (success.equals("成功")) {
            List<JSONObject> data = JSONArray.parseArray(responseJson.getString("data"), JSONObject.class);
            List<JSONObject> collect = new ArrayList<>();
            if (Objects.equals("1",type)){
                collect = data.stream().filter(d -> d.getString("mode").equals("2")&&d.getString("is_multi_select").equals("0")).collect(Collectors.toList());
            }else if  (Objects.equals("2",type)){
                collect = data.stream().filter(d -> d.getString("mode").equals("2")&&d.getString("is_multi_select").equals("1")).collect(Collectors.toList());
            }else if (Objects.equals("3",type)){
                collect = data.stream().filter(d -> d.getString("mode").equals("1")).collect(Collectors.toList());
            }
            for (JSONObject object : collect) {
                InterAttrRes interAttrRes = new InterAttrRes();
                String id = object.getString("id");
                String title = object.getString("title");
                String val = object.getString("itemValue");
                String required = object.getString("is_required");
                interAttrRes.setUuid(UUIDGenerator.generateUuid6());
                interAttrRes.setId(id);
                interAttrRes.setName(title);
                if (Objects.equals("1",required)){
                    interAttrRes.setRequired(true);
                }else {
                    interAttrRes.setRequired(false);
                }
                List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                List<JSONObject> jsonObjects = JSONObject.parseArray(val, JSONObject.class);
                for (JSONObject jsonObject1 : jsonObjects) {
                    InterAttrRes interAttrRes1 = new InterAttrRes();
                    String id1 = jsonObject1.getString("id");
                    interAttrRes1.setUuid(UUIDGenerator.generateUuid6());
                    interAttrRes1.setId(id1);
                    String title1 = jsonObject1.getString("title");
                    interAttrRes1.setName(title1);
                    interAttrResList1.add(interAttrRes1);
                }
                interAttrRes.setInterServerRes(interAttrResList1);
                interAttrResList.add(interAttrRes);
            }
        }
        return interAttrResList;
    }

    public InterServerRes builderInterServerResChild(JSONObject datum) {
        List<InterServerRes> list = new ArrayList<>();

        InterServerRes interServerRes = new InterServerRes();
        interServerRes.setId(datum.getString("id"));
        interServerRes.setName(datum.getString("name"));

        if (CollectionUtils.isNotEmpty(datum.getJSONArray("server"))) {
            List<JSONObject> children = JSONObject.parseArray(datum.getString("server"), JSONObject.class);
            for (JSONObject child : children) {
                list.add(builderInterServerResChild(child));
            }
        }
        interServerRes.setInterServerRes(list);
        return interServerRes;
    }

}